/**
 * Copyright (c) 2014-now, secondriver (secondriver@yeah.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package secondriver.jfinal.rack.kit;

import java.util.Hashtable;
import java.util.Map;

import com.jfinal.kit.StrKit;

import org.apache.log4j.Logger;

/**
 * Get System Values by SysValKit
 * 
 * @author ZhangXiao
 * 
 */
public class RockSysValKit {

	private static final Map<String, String> systemKeyValues = new Hashtable<String, String>(
			20);

	private static Logger logger = Logger.getLogger(RockSysValKit.class);

	public static int getInt(String name) {
		return Integer.parseInt(systemKeyValues.get(name));
	}

	public static int getInt(String name, int defaultValue) {
		String value = systemKeyValues.get(name);
		return StrKit.isBlank(value) ? defaultValue : Integer.parseInt(value);
	}

	public static boolean getBoolean(String name) {
		String value = systemKeyValues.get(name);
		if (StrKit.notNull(value)) {
			if (value.trim().equalsIgnoreCase("true")) {
				return true;
			}
		}
		return false;
	}

	public static boolean getBoolean(String name, boolean defaultValue) {
		String value = systemKeyValues.get(name);
		if (StrKit.notNull(value)) {
			if (value.trim().equalsIgnoreCase("true")) {
				return true;
			} else {
				return false;
			}
		} else {
			return defaultValue;
		}
	}

	public static String get(String name) {
		return systemKeyValues.get(name);
	}

	public static String get(String name, String defaultValue) {
		String value = systemKeyValues.get(name);
		return StrKit.isBlank(value) ? defaultValue : value;
	}

	public static void add(String name, String value) {
		if (systemKeyValues.containsKey(name)) {
			if (logger.isDebugEnabled()) {
				logger.debug("ConfigKit system configuration record exist the name, but will override the value.");
			}
		}
		systemKeyValues.put(name, value);
	}

	public static int configSize() {
		return systemKeyValues.size();
	}

	public static void configClear() {
		if (!systemKeyValues.isEmpty()) {
			systemKeyValues.clear();
		}
	}

}
